package com.lft.juc06.subsidiary;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 计数器——“加法计数器”
 * 集齐 7 颗龙珠，召唤神龙。
 */
public class TestCyclicBarrier {
	public static void main(String[] args) {
		// 召唤龙珠的线程
		CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> System.out.println("召唤出神龙~~~"));
		
		for (int i = 1; i <= 7; i++) {
			final int temp = i;
			new Thread(() -> {
				System.out.println(Thread.currentThread().getName() + " 收集了第 " + temp + " 颗龙珠！");
				try {
					// 等待7个线程全部执行完成。
					cyclicBarrier.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (BrokenBarrierException e) {
					e.printStackTrace();
				}
			}, String.valueOf(i)).start();
		}
	}
}
